/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.tutwiz.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.validation.constraints.*;

/**
 *
 * @author vind
 */
@Entity
public class Tutorial implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @Column(nullable=false)
    
    private String title;
    @Column(nullable=false)
    private String description;
    
    @OneToMany(mappedBy="tutorial", cascade= CascadeType.ALL, fetch=FetchType.LAZY)
    private List<Step> steps = new ArrayList<Step>();
    
    @ManyToMany(cascade= CascadeType.PERSIST, fetch=FetchType.LAZY)
    @JoinTable(name="tutorial_tags", 
            joinColumns=@JoinColumn(name="tutorial_fk"), 
            inverseJoinColumns=@JoinColumn(name="tag_fk"))
    private List<Tag> tags = new ArrayList<Tag>();
    
    @OneToMany(mappedBy="tutorial", cascade= CascadeType.ALL, fetch=FetchType.LAZY)
    private List<Vote> votes = new ArrayList<Vote>();
    
    @ManyToOne(cascade= CascadeType.PERSIST)
    @JoinColumn(name="USER_FK") 
    private User createdBy;
    
    @Column(nullable=false)
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date createdAt;
    int rate=0;

    public Tutorial() {
    }

    public Tutorial(String title, String description) {
        this.title = title;
        this.description = description;
        this.createdAt = Calendar.getInstance().getTime();
    }

    public Tutorial(String title, String description, List<Tag> tags, User createdBy) {
        this.title = title;
        this.description = description;
        this.tags = tags;
        this.createdBy = createdBy;
        this.createdAt = Calendar.getInstance().getTime();
    }
    
    public void setSteps(List<Step> steps){
        this.steps = steps;
    }
    
    public List<Step> getSteps(){
        return steps;
    }
    
    
    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public User getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(User createdBy) {
        this.createdBy = createdBy;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Long getId() {
        return id;
    }

    public int getRate() {
        return rate;
    }
    
    public void addVote(Vote v){
        votes.add(v);
        setRate();
    }
    
    private void setRate() {
        for(Vote v : votes){
            if(v.getOpinion()){
                ++rate;
            }else{
                --rate;
            }
        }
    }
    
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Collection<Tag> getTags() {
        return tags;
    }

    public void setTags(List<Tag> tags) {
        this.tags = tags;
    }

    public List<Vote> getVotes() {
        List<Vote> newVotes = new ArrayList<Vote>(this.votes);
        return newVotes;
    }
    
    public void vote(Vote newVote){
         for( Vote v : votes) {
           
            if (newVote.equals(v)){
                v.setOpinion(newVote.getOpinion());
            }else{
                votes.add(newVote);
            }
        }
        setRate();
    }

    public void addTag(Tag tag) {
        if(!tags.contains(tag)){
            tags.add(tag);
        }
    }

}
